﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
namespace QuanLyThuVien.Supporter
{
    //-----------------------------------------
    //Desc: Enum chứa các thao tác của người dùng: thêm, sửa xóa
    //-----------------------------------------
    public enum THAOTAC
    {
        THEM,
        SUA,
        XOA,
        XEM
    }
    public static class Utils
    {
        //-----------------------------------------
        //Desc: Mã hóa chuỗi dùng giải thuật md5
        //-----------------------------------------
        public static byte[] encryptData(string data)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashedBytes;
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(data));
            return hashedBytes;
        }

        //-----------------------------------------
        //Desc: Mã hóa chuỗi
        //-----------------------------------------
        public static string MaHoa(string data)
        {
            return BitConverter.ToString(encryptData(data)).Replace("-", "").ToLower();
        }

        //-----------------------------------------
        //Desc: Đọc file excel
        //-----------------------------------------
        public static DataTable ReadExcelFile(string fileName)
        {
            OleDbConnection oleDbConn = new OleDbConnection();
            string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
                + fileName + ";Extended Properties='Excel 12.0;'";
            oleDbConn = new OleDbConnection(strConn);
            string query = @"Select * FROM [Sheet1$]";
            try
            {
                oleDbConn.Open();
            }
            catch { return null; }
            OleDbCommand oleDbCmd = new OleDbCommand(query, oleDbConn);
            OleDbDataAdapter oleDbAdapter = new OleDbDataAdapter(oleDbCmd);
            DataTable dt = new DataTable();
            try
            {
                oleDbAdapter.Fill(dt);
            }
            catch
            {
                oleDbConn.Close();
                return null;
            }
            oleDbConn.Close();
            return dt;
        }

        //-----------------------------------------
        //Desc: Kiểm tra keycode
        //-----------------------------------------
        public static bool CheckKeyCode(int keycode)
        {
            if ((keycode >= 48 && keycode <= 57) || keycode == 45 || keycode == 46 || keycode == 8)
                return true;
            return false;
        }

        //-----------------------------------------
        //Desc: Kiểm tra ký tự đặc biệt
        //-----------------------------------------
        public static bool IsSpecialKey(int keycode)
        {
            if ((keycode >= 33 && keycode <= 47) || (keycode >= 58 && keycode <= 64)
                || (keycode >= 91 && keycode <= 96) || (keycode >= 123 && keycode <= 126))
                return true;
            return false;
        }

        //-----------------------------------------
        //Desc: xóa khoảng trằng thừa
        //-----------------------------------------
        public static string ClearSpace(string input)
        {
            int i = 0;
            string output = input;
            while (i < output.Length - 1)
            {
                if (output[i] == ' ' && output[i + 1] == ' ')
                    output = output.Remove(i, 1);
                else
                    i++;
            }
            return output.Trim();
        }

        //-----------------------------------------
        //Desc: hàm cắt chuỗi
        //-----------------------------------------
        public static List<string> SplitString(string str)
        {
            List<string> list = new List<string>();
            int i = 0;
            int j = 0;
            try
            {
                while (i < str.Length)
                {
                    while (i < str.Length && str[i] == ' ')
                        i++;
                    j = i;
                    while (j < str.Length && str[j] != ' ')
                        j++;
                    if (j != i)
                    {
                        string s = str.Substring(i, j - i).ToLower();
                        if (!list.Contains(s))
                            list.Add(s);
                        i = j;
                    }
                }
            }
            catch { }

            return list;
        }
    }
}
